﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ExercisesResolution.Models;

namespace ExercisesResolution.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        [ActionName("Content")]
        public ActionResult ContentExec(string id)
        {
            Image image = new Image { Title = "No image selected" };
            if (TempData["Image"] != null)
                image = (Image)TempData["Image"];
            return View(image);
        }

        [AcceptVerbs("GET")]
        public ActionResult Edit()
        {
            SelectList select = new SelectList(
                new ImageRepository().GetLicenseList(),
                "Id",
                "Name");
            ViewData["AvailableLicenses"] = select;
            return View();
        }

        [AcceptVerbs("POST")]
        public ActionResult Edit(Image image)
        {
            TempData["Message"] = "Image saved!";
            if (image.License != null)
                image.License = new ImageRepository().GetLicenseById(image.License.Id);
            image.CreationDate = DateTime.Now;
            TempData["Image"] = image;
            return RedirectToAction("Content");
        }

        public ActionResult Index()
        {
            return RedirectToAction("Edit");
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
